﻿using System;
using System.Diagnostics;
using System.IO;

namespace SDG.UnitTesting
{
    public class SDGConsoleWrapper
    {
        private string _fileName;
        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        private string _conApplication;
        public string ConsoleApplication
        {
            get { return _conApplication; }
            set { _conApplication = value; }
        }

        public SDGConsoleWrapper(string fileName) : this(Properties.Settings.Default.DefaultConsoleExe, fileName)
        { }

        public SDGConsoleWrapper(string exe, string fileName)
        {
            if (string.IsNullOrEmpty(exe)) throw new ArgumentNullException("exe");
            if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName");

            FileName = fileName;
            ConsoleApplication = exe;
        }

        public void Execute()
        {
            if(!File.Exists(ConsoleApplication))
                throw new FileNotFoundException("Console application could not be found.", ConsoleApplication);

            if(!File.Exists(FileName))
                throw new FileNotFoundException("Could not find the project file", FileName);

            string arguments = " /project:\"" + FileName + "\"";

            Console.WriteLine("Executing: {0} {1}", ConsoleApplication, arguments);

            ProcessStartInfo psi = new ProcessStartInfo(ConsoleApplication, arguments);
            psi.RedirectStandardOutput = true;
            psi.UseShellExecute = false;
            psi.CreateNoWindow = true;
            psi.WindowStyle = ProcessWindowStyle.Hidden;

            Process p = new Process();
            p.StartInfo = psi;
            p.Start();
            p.WaitForExit();
        }
    }
}
